#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "AD7793.h"
#include "Thermocouple.h"

int main(void)
{
	float tc_voltage;
	float pt100_temp;
	float hot_junction_temp;
	uint8_t ad7793_id;
	uint32_t raw_data;
	
	/*ģ���ʼ��*/
	OLED_Init();						//OLED��ʼ��
	Serial_Init();						//���ڳ�ʼ��
	AD7793_Init();						//AD7793��ʼ��
	Thermocouple_Init();				//�ȵ�żģ���ʼ��
	
	Serial_Printf("\r\n=== K���ȵ�ż�¶Ȳ���ϵͳ ===\r\n");
	
	// ��ȡAD7793 ID
	ad7793_id = AD7793_GetID();
	Serial_Printf("AD7793 ID: 0x%02X\r\n", ad7793_id);
	
	// ���DRDY����״̬
	Serial_Printf("DRDY����״̬: %d\r\n", GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3));
	
	Serial_Printf("AD7793��ʼ�����\r\n");
	Serial_Printf("��ʼ�¶Ȳ���...\r\n\r\n");
	
	while (1)
	{
		// ��ʼ���ȵ�żͨ������ȡ�ȵ�ż��ѹ
		Serial_Printf("��ʼ���ȵ�żͨ��...\r\n");
		AD7793_ThermocoupleInit();
		Delay_ms(500);
		
		// ���DRDY״̬
		Serial_Printf("�ȵ�żDRDY״̬: %d\r\n", AD7793_DataReady());
		
		// ��ȡԭʼ����
		raw_data = AD7793_ReadRawData();
		Serial_Printf("�ȵ�żԭʼ����: 0x%06lX\r\n", raw_data);
		
		tc_voltage = AD7793_GetThermocoupleValue();
		
		// ��ʼ��PT100ͨ������ȡ����¶�
		Serial_Printf("��ʼ��PT100ͨ��...\r\n");
		AD7793_PT100Init();
		Delay_ms(500);
		
		// ���DRDY״̬
		Serial_Printf("PT100 DRDY״̬: %d\r\n", AD7793_DataReady());
		
		// ��ȡԭʼ����
		raw_data = AD7793_ReadRawData();
		Serial_Printf("PT100ԭʼ����: 0x%06lX\r\n", raw_data);
		
		pt100_temp = AD7793_GetPT100Temperature();
		
		// �����ȵ�ż�ȶ��¶ȣ�������˲�����
		hot_junction_temp = Thermocouple_GetTemperature(tc_voltage, pt100_temp);
		
		// ͨ�����ڷ��Ͳ������
		Serial_Printf("�ȵ�ż��ѹ: %.4f V\r\n", tc_voltage);
		Serial_Printf("�ȵ�ż��ѹ: %.3f mV\r\n", tc_voltage * 1000.0f);
		Serial_Printf("����¶�(PT100): %.2f ��C\r\n", pt100_temp);
		Serial_Printf("�ȶ��¶�: %.2f ��C\r\n", hot_junction_temp);
		Serial_Printf("--------------------------------\r\n");
		
		// ��ʱ1��
		Delay_ms(1000);
	}
}
